<?php

namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

class GiteePullerJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
    private $proejctName;
    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($proejctName)
    {
        //
        $this->proejctName=$proejctName;
        $this->onQueue("gitee_puller");
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        $dir=dirname(__DIR__);
        $dir=dirname($dir);
        $targetDir="/www/wwwroot/online-shop";
        $targetBranch="dev";
        $origin="origin-ssh";
        $sh="cd {$targetDir} && git pull origin{$targetBranch}";
        if($this->proejctName=="hyperfapi"){
         $targetDir="/www/wwwroot/hyperfapi";
         $targetBranch="master";
        }
        if($this->proejctName=="zdalive"){
         $targetDir="/www/wwwroot/zdalive";
         $targetBranch="master";
         $origin="origin-ssh";
        }
        $sh="cd {$targetDir} && git pull {$origin} {$targetBranch}";
        $log=$dir.DIRECTORY_SEPARATOR."{$this->proejctName}.gitee.pull.log";
        $str=PHP_EOL."【job】".date("Y-m-d H:i:s").PHP_EOL;
        $s123=exec($sh,$data123);
        $str.=$s123.PHP_EOL.json_encode($data123).PHP_EOL;
        file_put_contents($log,$str,FILE_APPEND);
    }
}
